<template>
	<view class="content">
		<button type="primary" @click="btnTakePhoto">拍照或从相册选择</button>
		<image :src="imagepath"></image>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: 'Hello',
				imagepath: '',
			}
		},
		onLoad() {
			this.getApi()
			this.getUniCloudFunc()
		},
		methods: {
			// 获取一张图片
			btnTakePhoto() {
				// 选择图片
				uni.chooseImage({
					count: 1,
					success: (res) => {
						console.log(res)
						this.imagepath = res.tempFilePaths[0];
						this.image2base64(this.imagepath);
					}
				})
			},
			// 图片转换格式
			image2base64(path) {
				uni.getFileSystemManager().readFile({
					filePath: this.imagepath,
					encoding: "base64",
					success: (res) => {
						console.log(res);
					}
				});
			},
			// 请求接口
			async getApi(b64) {
				var [err, rs] = await uni.request({
					url: 'http://www.baidu.com'
				})
				console.log(rs)
			},
			
			// 调用 uniCloud 云函数
			async getUniCloudFunc(b64) {
				uniCloud.callFunction({
					name:'ImageClassify',
					data:{
						name:'小明'
					},
					success:(res) => {
						console.log('云函数结果',res)
					}
				})
			},
		}
	}
</script>

<style scoped>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
</style>
